package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.conmon.MyPageData;
import com.woniuxy.conmon.enums.SystemCode;
import com.woniuxy.pojo.NewsArticle;
import com.woniuxy.pojo.dto.NewsArticleDto;
import com.woniuxy.pojo.vo.AuditVO;
import com.woniuxy.service.NewsArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/audit")
public class AuditController {
    @Autowired
    private NewsArticleService newsArticleService;
    @PostMapping("/getNewsByStatus/{pageNum}/{pageSize}")
    public ResponseEntity<?> getNewsByStatus(@PathVariable Integer pageNum,
                                             @PathVariable Integer pageSize,
                                             @RequestBody NewsArticleDto dto){
       Page<AuditVO> page = newsArticleService.getNewsByStatus(pageNum,pageSize,dto);
       MyPageData data = new MyPageData(page.getTotal(), page.getRecords());
       return ResponseEntity.ok(data);
    }

    @PutMapping("/approved/{id}")
    public ResponseEntity<?> approved(@PathVariable Integer id){
        LambdaUpdateWrapper<NewsArticle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(NewsArticle::getId,id).set(NewsArticle::getStatus,3);
        boolean flag = newsArticleService.update(wrapper);
        return ResponseEntity.ok(flag? SystemCode.PASS_SUCCESS.getMessage(): SystemCode.FAIL.getMessage());
    }
    @PostMapping("/reject/{id}")
    @Transactional
    public ResponseEntity<?> reject(@PathVariable Integer id,@RequestBody String remark){
        LambdaQueryWrapper<NewsArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NewsArticle::getId,id);
        NewsArticle newsArticle = newsArticleService.getOne(wrapper);
        newsArticle.setStatus(0);
        newsArticle.setRemark(remark);
        newsArticleService.updateById(newsArticle);
        return ResponseEntity.ok(SystemCode.REVIEW_FAILED.getMessage());
    }

}
